package com.fesine.ch01.lesson04.threadsafe.controller;

/**
 * 模拟非线程安全环境
 * 本类模拟成一个Servlet组件
 * Created by Fesine on 2016/6/26.
 */
public class LoginServlet {
    private static String usernameRef;
    private static String passwordRef;

    //使用synchronized，解决非线程安全
    synchronized public static void doPost(String username, String password) {
        try {
            usernameRef = username;
            if (username.equals("a")) {
                Thread.sleep(5000);
            }
            passwordRef = password;
            System.out.println("username=" + usernameRef+" password=" + passwordRef);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
